<script setup>
import {
  ref,
  onMounted,
  inject,
  getCurrentInstance,
  onBeforeUpdate,
} from "vue";
const currentInstance = getCurrentInstance();
const props = defineProps(["index", "title", "icon"]);
const length = ref(0);
const index = ref(props.index);
const provideProps = inject("provideProps");
onMounted(() => {
  length.value = document.querySelectorAll(".step").length;
});

onBeforeUpdate(() => {});
</script>

<template>
  <div :class="['step', { 'step-end': length - 1 == index }]" ref="stepDom">
    <div class="step-box">
      <div
        class="step-item"
        :style="{
          border:
            index <= provideProps.active
              ? ` 2px solid ${provideProps.color}`
              : '',
          color: index <= provideProps.active ? ` ${provideProps.color}` : '#aaa',
          width: provideProps.width + 'px',
          height: provideProps.height + 'px',
          lineHeight: provideProps.height + 'px',
        }"
      >
        <span v-if="props.icon"><i :class="'iconfont ' + props.icon"></i></span>
        <span v-else>{{ Number(index) + 1 }}</span>
      </div>

      <div v-if="index < length - 1" class="step-row">
        <div
          class="step-row-bg"
          :style="{
            backgroundColor: provideProps.color,
            width:
              index < provideProps.active
                ? '100%'
                : index == provideProps.active
                ? '50%'
                : index > provideProps.active
                ? '0'
                : '',
            transition:
              index == provideProps.active ? 'all 0.5s linear 0.5s' : '',
          }"
        ></div>
      </div>
    </div>
    <div
      class="step-title"
      :style="{
        width: provideProps.width + 'px',
        color: index <= provideProps.active ? ` ${provideProps.color}` : '',
      }"
    >
      {{ props.title }}
    </div>
  </div>
</template>

<style lang="scss">
.step {
  flex-grow: 1;
  .step-box {
    width: 100%;
    display: flex;
    align-items: center;
    .step-item {
      // box-sizing: content-box;
      width: 70px;
      height: 70px;
      text-align: center;
      line-height: 70px;
      font-size: 18px;
      font-weight: 700;
      border: 2px solid #ddd;
      border-radius: 50%;
      transition: all 2s linear 0.5;
    }

    .step-row {
      flex-grow: 1;
      height: 2px;
      background-color: #ddd;

      .step-row-bg {
        height: 100%;
        background-color: #1abc9c;
        transition: all 0.5s linear;
      }
    }
  }

  &.step-end {
    flex-grow: 0;
  }

  .step-title {
    width: 70px;
    padding: 20px 0;
    text-align: center;
    font-size: 12px;
  }
}
</style>
